package com.jn.mvp.views;

import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.jn.mvp.R;
import com.jn.mvp.base.BaseActivity;
import com.jn.mvp.contract.HanZiContract;
import com.jn.mvp.entity.HanZi;
import com.jn.mvp.presenter.HanZiPresenter;
import com.jn.mvp.utils.LogUtils;

public class HanZiActivity extends BaseActivity implements HanZiContract.IHanZiView, View.OnClickListener {

    private ImageView mImage;
    private EditText mEdit;
    private Button mButton;
    private TextView mText;
    private TextView mText1;

    private HanZiContract.IHanZiPresenter mPresenter;

    @Override
    public int getLayoutId() {
        return R.layout.activity_hanzi;
    }

    @Override
    public void init() {
        mPresenter = new HanZiPresenter(this, this);
        mImage = findViewById(R.id.image);
        mEdit = findViewById(R.id.edit);
        mButton = findViewById(R.id.button);
        mButton.setOnClickListener(this);
        mText = findViewById(R.id.text);
        mText1 = findViewById(R.id.text1);
    }

    @Override
    public void showData(HanZi hanZi) {
        LogUtils.d("asdf", "===" + hanZi.toString());
        Glide.with(this).asGif().load(hanZi.getAnimation()).into(mImage);
        mText.setText(hanZi.getBisbun() + "\n(共 " + hanZi.getStroke_count() + " 画)");
        mText1.setText(hanZi.getPinyin());
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.button) {
            String input = mEdit.getText().toString();
            if (!TextUtils.isEmpty(input)) {
                if (mPresenter != null) {
                    mPresenter.getData(input);
                }
            } else {
                showToast("请输入汉字");
            }
            mEdit.setText("");
        }
    }

    @Override
    public void onHttpLoading(boolean loading) {

    }

    @Override
    public void onHttpFail(String... msg) {
        showToast(msg.length > 0 ? msg[0] : "");
    }

    @Override
    public void onHttpTimeout() {

    }

    @Override
    public void onHttpTokenExpiration() {

    }
}